package com.zbb.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbb.base.model.PageParams;
import com.zbb.base.model.PageResult;
import com.zbb.base.model.Result;
import com.zbb.order.feignclient.UserServiceClient;
import com.zbb.order.mapper.LabelMapper;
import com.zbb.order.mapper.LabelRelationMapper;
import com.zbb.order.mapper.OrderInitiateMapper;
import com.zbb.order.model.dto.OrderInitiateDto;
import com.zbb.order.model.dto.QueryOrderInitiateDto;
import com.zbb.order.model.po.Label;
import com.zbb.order.model.po.LabelRelation;
import com.zbb.order.model.po.OrderInitiate;
import com.zbb.order.model.po.User;
import com.zbb.order.service.OrderInitiateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderInitiateServiceImpl implements OrderInitiateService {
    @Autowired
    OrderInitiateMapper orderInitiateMapper;

    @Autowired
    UserServiceClient userServiceClient;

    @Autowired
    LabelRelationMapper labelRelationMapper;

    @Autowired
    LabelMapper labelMapper;

    @Override
    public PageResult<OrderInitiateDto> selectOrderInitiate(PageParams pageParams, QueryOrderInitiateDto queryOrderInitiateDto) {
        Page<OrderInitiateDto> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        IPage<OrderInitiateDto> orderInitiateDtoIPage = orderInitiateMapper.selectOrderInitiate(page, queryOrderInitiateDto);
        List<OrderInitiateDto> list = orderInitiateDtoIPage.getRecords();
        list = getUserInfo(list);
        long total = orderInitiateDtoIPage.getTotal();
        PageResult<OrderInitiateDto> orderInitiateDtoPageResult = new PageResult<>(list, total,
                pageParams.getPageNo(), pageParams.getPageSize());
        return orderInitiateDtoPageResult;
    }

    @Override
    public PageResult<OrderInitiateDto> selectOrderInitiateByHot(PageParams pageParams) {
        Page<OrderInitiate> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        LambdaQueryWrapper<OrderInitiate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInitiate::getStatus,0);
        queryWrapper.orderByDesc(OrderInitiate::getHotness);
        Page<OrderInitiate> orderInitiatePage = orderInitiateMapper.selectPage(page, queryWrapper);
        List<OrderInitiate> records = orderInitiatePage.getRecords();
        List<OrderInitiateDto> orderInitiateDtoList = copyList(records);
        orderInitiateDtoList = getUserInfo(orderInitiateDtoList);
        long total = page.getTotal();
        PageResult<OrderInitiateDto> orderInitiateDtoPageResult = new PageResult<>(orderInitiateDtoList, total,
                pageParams.getPageNo(), pageParams.getPageSize());
        return orderInitiateDtoPageResult;
    }


    public  List<OrderInitiateDto> getUserInfo(List<OrderInitiateDto> list){
        for (OrderInitiateDto orders : list) {
            //根据用户id获取用户信息
            User user = userServiceClient.querytUser(orders.getUserId());
            orders.setUserName(user.getUserName());
            orders.setTouUrl(user.getPhotoUrl());
            //根据订单详情id获取标签
            QueryWrapper<LabelRelation> wrapperLabel = new QueryWrapper<>();
            wrapperLabel.eq("initiate_id", orders.getId());
            List<LabelRelation> labelRelations = labelRelationMapper.selectList(wrapperLabel);
            List<Label> labels = new ArrayList<>();
            for (LabelRelation labelNos : labelRelations) {
                Label label = labelMapper.selectById(labelNos.getLabelId());
                labels.add(label);
            }
            orders.setLableList(labels);
        }
        return list;

    }
    private List<OrderInitiateDto> copyList(List<OrderInitiate>records) {
        List<OrderInitiateDto> orderInitiateDtoList=new ArrayList<>();
        for (OrderInitiate orderInitiate : records) {
            orderInitiateDtoList.add(copy(orderInitiate));
        }

        return orderInitiateDtoList;



    }
    private  OrderInitiateDto copy(OrderInitiate orderInitiate){
        OrderInitiateDto orderInitiateDto=new OrderInitiateDto();

        BeanUtils.copyProperties(orderInitiate,orderInitiateDto);
        return orderInitiateDto;
    }






}